import {auditJoinGet,auditJoinBatchOperate,auditJoinOperate,getCourseGroupInfo} from "../../api/auditJoin";
import {getTeacherInfo} from "../../api/auditRegister";

const state = {
    //加入信息
    auditJoinInfo:[],
    //信息总数
    total:0,
    //老师详细信息
    teacherInfo:{
            teacherId: '',
            account: '', //工号
            name: '', //老师姓名
            password: '', //老师密码
            college: '',//学院
            e_mail: '',//邮箱
            phone: ''//电话
        },
    courseGroupInfo:{
            simpleCourseGroupInfo:{
                courseGroupId:'',
                courseGroupTime:'',
                course:'',
                teachers:[],
                classes:[]
            },
            courseGroupName:'',
            courseGroupLeader:{
                teacherId:'',
                teacherNumber:'',
                teacherName:''
            }
    },
}

const mutations = {
    // mutations改变state中的数据
    SET_AUD_JOIN_INFO: (state, info) => { // 存储token
        const array=[];
        for(let i of info){
            //返回数据做进一步处理
            array.unshift({
                key: i.auditJoinId,
                content: '工号为 '+i.teacherAccount+"的 "+i.teacherName+"老师 申请加入"+
                    i.courseGroupTime+i.course+'课程组',
                result: i.result===1 ? '未审核':(i.result===2 ? "已通过": "已驳回"),
                teacherId: i.teacherId,
                courseGroupId: i.courseGroupId,
            })
        }
        state.auditJoinInfo=array;
    },
    SET_TOTAL: (state,total) => {
        state.total=total;
    },
    //空方法
    NULL: () => {},
    SET_TEACHER_INFO: (state,teachRecord)=>{
        state.teacherInfo.teacherId=teachRecord.simpleTeacherInfo.teacherId
        state.teacherInfo.account=teachRecord.simpleTeacherInfo.account
        state.teacherInfo.name=teachRecord.simpleTeacherInfo.name
        state.teacherInfo.password=teachRecord.simpleTeacherInfo.password
        state.teacherInfo.college=teachRecord.simpleTeacherInfo.college
        state.teacherInfo.e_mail=teachRecord.email
        state.teacherInfo.phone=teachRecord.phone
    },
    SET_COURSE_GROUP_INFO: (state,data)=>{
        state.courseGroupInfo.simpleCourseGroupInfo.classes=data.simpleCourseGroupInfo.classes
        state.courseGroupInfo.simpleCourseGroupInfo.teachers=data.simpleCourseGroupInfo.teachers
        state.courseGroupInfo.simpleCourseGroupInfo.courseGroupTime=data.simpleCourseGroupInfo.time
        state.courseGroupInfo.simpleCourseGroupInfo.course=data.simpleCourseGroupInfo.course
        state.courseGroupInfo.simpleCourseGroupInfo.courseId=data.simpleCourseGroupInfo.courseId
        state.courseGroupInfo.simpleCourseGroupInfo.courseGroupId=data.simpleCourseGroupInfo.courseGroupId
        state.courseGroupInfo.courseGroupName=data.name
        state.courseGroupInfo.courseGroupLeader.teacherId=data.leader.teacherId
        state.courseGroupInfo.courseGroupLeader.teacherNumber=data.leader.teacherNumber
        state.courseGroupInfo.courseGroupLeader.teacherName=data.leader.teacherName
    }
}

const actions = {
    // 发送请求
    // commit 是调用上面mutations中函数的，state是上面的state
    //调用引入的api方法，还用处理数据

    auditJoinGet({ commit },params){
        //console.log(params);
        return new Promise((resolve, reject) => {
            auditJoinGet(params).then( data => {
                //console.log(data)
                if(data.code === 1){ // 获取成功
                    commit('SET_AUD_JOIN_INFO', data.data.data);
                    //commit('SET_TOTAL',data.data.data.length);
                    //console.log("调用auditJoinGet成功", data);
                    resolve(data);
                }else{
                    reject(data.msg);
                }
            } ).catch(error=>{
                reject(error)
            })
        })
    },
    auditJoinOperate({commit},params){
        commit('NULL');
        return new Promise((resolve, reject) => {
            auditJoinOperate(params).then(data => {
                if(data.code === 1){ // 获取成功
                    //console.log("调用auditJoinOperate成功", data);
                    resolve(data);
                }else{
                    reject(data);
                }
            })
        })
    },
    auditJoinBatchOperate({commit},params){
        commit('NULL');
        return new Promise((resolve, reject) => {
            auditJoinBatchOperate(params).then(data => {
                if(data.code === 1){ // 获取成功
                    //console.log("调用auditJoinBatchOperate成功", data);
                    resolve(data);
                }else{
                    reject(data);
                }
            })
        })
    },
    getTeacherInfoAndCourseGroupInfo({commit},params){
        const {token,teacherId,courseGroupId}=params;
        return new Promise((resolve, reject) => {
            getTeacherInfo({token,teacherId}).then(data => {
                if(data.code === 1){ // 获取成功
                    //console.log("getTeacherInfo调用成功", data);
                    commit("SET_TEACHER_INFO",data.data);
                    getCourseGroupInfo({token,courseGroupId}).then(data => {
                        if(data.code===1){
                            //console.log("getCourseGroupInfo调用成功", data);
                            commit("SET_COURSE_GROUP_INFO",data.data);
                            resolve(data);
                        }else{
                            reject(data.msg)
                        }
                    })
                }else{
                    reject(data.msg);
                }
            })
        })
    },
}

export default {
    //为true，则可以模块化引用
    namespaced: true,
    state,
    mutations,
    actions
}
